package com.jcl.cloud.services.gateway.service;

import com.jcl.cloud.common.service.exception.JclCrawlException;
import com.jcl.cloud.services.gateway.common.task.CrawlTaskRsp;
import com.jcl.cloud.services.gateway.common.task.TaskContent;
import com.jcl.cloud.services.unicrawler.client.CrawlerManagerClient;
import com.jcl.cloud.services.unicrawler.common.dto.CrawlTaskMgrReq;
import com.jcl.cloud.services.unicrawler.common.dto.UniCrawlTaskRsp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CrawlerTaskService {

    @Autowired
    private CrawlerManagerClient crawlerManagerClient;




    public CrawlTaskRsp createTask(TaskContent taskContent) throws JclCrawlException {
        try {
            CrawlTaskMgrReq crawlTaskMgrReq = new CrawlTaskMgrReq();
            crawlTaskMgrReq.setTaskId(taskContent.getTaskId());
            crawlTaskMgrReq.setTenantId(taskContent.getTenantId());

            crawlTaskMgrReq.setAccountKey(taskContent.getAccountKey());
            crawlTaskMgrReq.setAccount(taskContent.getAccount());
            crawlTaskMgrReq.setUserName(taskContent.getUserName());
            crawlTaskMgrReq.setPassWord(taskContent.getPassWord());
            crawlTaskMgrReq.setSiteCode(taskContent.getSiteCode());
            crawlTaskMgrReq.setTaskType(taskContent.getTaskType());
            crawlTaskMgrReq.setCron(taskContent.getCron());
            crawlTaskMgrReq.setLevel(taskContent.getLevel());


            UniCrawlTaskRsp uniCrawlTaskRsp = this.crawlerManagerClient.createTask(crawlTaskMgrReq);
            CrawlTaskRsp crawlTaskRsp = new CrawlTaskRsp();

            crawlTaskRsp.setTraceId(taskContent.getTraceId());
            crawlTaskRsp.setTaskId(taskContent.getTaskId());
            crawlTaskRsp.setCrawlerId(uniCrawlTaskRsp.getCrawlerId());

            return crawlTaskRsp;
        } catch (Exception e) {
            throw new JclCrawlException();
        }
    }





}
